<?php
/*
  Microblog bot for game release notifications
  Copyright (C) 2017  Alexander Yakovlev

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once ("vendor/autoload.php");
use Symfony\Component\Yaml\Yaml;
use Revolution\Mastodon\MastodonClient;
use Mremi\UrlShortener\Provider\Bitly\BitlyProvider;
use Mremi\UrlShortener\Provider\Bitly\GenericAccessTokenAuthenticator;
require_once "Game.php";
require_once "Source.php";
require_once "download.php";

$loader = new \Aura\Autoload\Loader;
$loader->register();
$loader->addPrefix('Source', 'Source');

$parsers = [
  //    'urq',
  'anivisual',
  'kvester',
  'apero',
  'instead',
  'hyperbook_ru',
  'hyperbook_en',
  'questbook',
  'textadventures',
  'ifdb',
  'dashingdon',
  'itch',
  'gamejolt',
  'steam',
];

$config = Yaml::parse(file_get_contents('config.yml'));
$lastrun = 0;
if (file_exists('.lastrun') && !$config['DRY_RUN']) {
  $lastrun = file_get_contents('.lastrun');
}

$pandoc = new \Pandoc\Pandoc();
$bitlyProvider =  new BitlyProvider(
  new GenericAccessTokenAuthenticator($config['BITLY_TOKEN'])
);
if ($config['TELEGRAM'] === true) {
  $telegram = new Longman\TelegramBot\Telegram(
    $config['TELEGRAM_API_KEY'],
    $config['TELEGRAM_BOT_NAME']
  );
}
if ($config['MASTODON'] === true) {
  $mastodon = new MastodonClient();
}

function check($classname, $command) {
  global $parsers;
  global $mastodon;
  global $telegram;
  global $config;
  if (in_array($command, $parsers)) {
    $cname = 'Source\\'.$classname;
    $source = (new $cname());
    $source->check();
    if (empty($source->games)) {
      return;
    }
    foreach ($source->games as $game) {
      $description = $game->text();
      if ($config['DRY_RUN']) {
        echo $description."\n";
        continue;
      }
      if ($config['TELEGRAM'] === true) {
        try {
          $result = \Longman\TelegramBot\Request::sendMessage([
            'chat_id' => $config['TELEGRAM_CHAT_ID'],
            'text' => $description,
            'parse_mode' => 'Markdown'
          ]);
          if (!$config['DRY_RUN']) {
            file_put_contents('.lastrun', time());
          }
        } catch (Longman\TelegramBot\Exception\TelegramException $e) {
          echo $e;
        }
      }
      if ($config['MASTODON'] === true) {
        $mastodon->domain($config['MASTODON_SERVER'])->token($config['MASTODON_ACCESS_TOKEN']);
        /*
          if ($image) {
            download('https://ifhub.club'.$image, './'.basename($image));
            $attachment = $mastodon->post('/media', [
              'file' => file_get_contents('./'.basename($image))
            ]);
            var_dump($attachment);
            unlink('./'.basename($image));
            $mdescription .= $attachment->url;
          }
         */
        $mastodon->status_post($description);
        if (!$config['DRY_RUN']) {
          file_put_contents('.lastrun', time());
        }
      }
    }
  }
}

foreach($config['parsers'] as $parser) {
  $command = strtolower($parser['classname']);
  if (isset($parser['command'])) {
    $command = $parser['command'];
  }
  check ($parser['classname'], $command);
}
